package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.Industry;
import com.csjy.migration.hnnd.dict.IndustryCode;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 客户表（自然人）
 */
@BusinessDataConverter(
        auto = false,

        insertSql = "insert into nd_person_info (ID, CUSTOMER_ID, SEX, BIRTHDAY, EDUCATE, ID_CARD, REGIST_DEPART, WORK_DEPART, PHONE, FAMILY_TEL, INCOME, WORK_COMPANY, WORK_INDUSTRY, WORK_JOB, WORK_ADDRESS, WORK_PHONE, HANDLE_TIME, MARRAY, MATE_NAME, MATE_ID_CARD, MATE_PROFESSION, ESTATE, PERSONAL_IMP_MSG, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, IS_BLACKLIST, BLACKLIST_MSG, INDUSTRY_CODE)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",

        updateSql = "UPDATE nd_person_info SET CUSTOMER_ID = ?,SEX = ?,BIRTHDAY = ?,EDUCATE = ?,ID_CARD = ?,REGIST_DEPART = ?,WORK_DEPART = ?,PHONE = ?,FAMILY_TEL = ?,INCOME = ?,WORK_COMPANY = ?,WORK_INDUSTRY = ?,WORK_JOB = ?,WORK_ADDRESS = ?,WORK_PHONE = ?,HANDLE_TIME = ?,MARRAY = ?,MATE_NAME = ?,MATE_ID_CARD = ?,MATE_PROFESSION = ?,ESTATE = ?,PERSONAL_IMP_MSG = ?,CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,IS_BLACKLIST = ?,BLACKLIST_MSG = ?,INDUSTRY_CODE = ? WHERE ID = ?",

        findMigratedIdListSql = "SELECT ID FROM nd_person_info "
)
public class NdPersonInfo  extends AbstractBusiness {


    public NdPersonInfo(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }
    @Override
    public void findMigratedIdList() throws SQLException {
        this.migratedIdList = this.targetDbUtil.selectSingleColumn(findMigratedIdListSql, new ArrayList());
    }
    @Override
    public void migrate() throws ScriptException, SQLException {

    }
    public void customMigrate(Map row) throws Exception {
        int needMigratedResult = this.needMigrated(row.get("ENTERPRISENO").toString());
        if(needMigratedResult == 0) {
            return;
        }

        try{

            if(needMigratedResult == 1) {
                this.targetDbUtil.updateByParams(updateSql,this.buildValues(row),true);
//            this.batchUpdateValues.add(this.buildValues(row));
            } else {
                this.targetDbUtil.updateByParams(insertSql,this.buildValues(row),false);
//            this.batchInsertValues.add(this.buildValues(row));
            }
        }catch (Exception e) {
            throw new Exception(e);
        }


    }
    public List buildValues(Map btEnterpriseBasic)  throws SQLException {

        List values = new ArrayList();
        //ID
        values.add(btEnterpriseBasic.get("ENTERPRISENO").toString());
        //CUSTOMER_ID	客户id
        values.add(btEnterpriseBasic.get("ENTERPRISENO").toString());
        //SEX	客户性别
        values.add(btEnterpriseBasic.get("FACTCOLLECTCAPITAL"));
        //BIRTHDAY	出生日期
        values.add(btEnterpriseBasic.get("ESTABLISHTIME"));
        //EDUCATE	客户学历
        values.add(Tool.covertToBigDecimal(btEnterpriseBasic.get("FAX")));
        //ID_CARD	身份证号
        values.add(btEnterpriseBasic.get("BUSIREGISTCODE"));
        //REGIST_DEPART	登记机关
        values.add(btEnterpriseBasic.get("LOGADDRESS"));
        //WORK_DEPART	单位性质
        values.add(Tool.covertToBigDecimal(btEnterpriseBasic.get("ZIPCODE")));
        //PHONE	联系手机
        values.add(btEnterpriseBasic.get("LINKMANCELLPHONE"));
        //FAMILY_TEL	家庭电话
        values.add(btEnterpriseBasic.get("LINKMANDUTY"));
        // INCOME	月收入元
        values.add(BusinessTool.conMoeny(btEnterpriseBasic.get("CORPORATIONTEL")));
        //WORK_COMPANY	工作单位
        values.add(btEnterpriseBasic.get("SUBJECTIONRELATION"));
        //WORK_INDUSTRY	从事行业
        values.add(DictTool.getTargetDictCode(Industry.class,btEnterpriseBasic.get("AFFTRADE")));
        //WORK_JOB	担任职务
        values.add(btEnterpriseBasic.get("CORPORATION"));
        //WORK_ADDRESS	单位地址
        values.add(btEnterpriseBasic.get("EMAIL"));
        //WORK_PHONE	办公电话
        values.add(btEnterpriseBasic.get("LINKMANTEL"));
        //HANDLE_TIME	受理时间
        values.add(null);
        //MARRAY	婚姻状况
        values.add(Tool.covertToBigDecimal(btEnterpriseBasic.get("REGISTCAPITAL")));
        //MATE_NAME	配偶姓名
        values.add(btEnterpriseBasic.get("LINKMAN"));
        //MATE_ID_CARD	身份证号（配偶）
        values.add(btEnterpriseBasic.get("CORPORATIONCODE"));
        //MATE_PROFESSION	配偶职业
        values.add(btEnterpriseBasic.get("MATEDUTYINFO"));
        //ESTATE	财产情况
        values.add(btEnterpriseBasic.get("DOUBLE_DEALAREA"));
        //PERSONAL_IMP_MSG	个人重要情况
        values.add(null);
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btEnterpriseBasic.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btEnterpriseBasic.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);
        //IS_BLACKLIST	黑名单
        values.add(null);
        //BLACKLIST_MSG	黑名单说明
        values.add(null);
        //INDUSTRY_CODE	行业code
        values.add(DictTool.getTargetDictCode(IndustryCode.class,btEnterpriseBasic.get("AFFTRADE")));
        return values;
    }
}


